package com.cw.jeeyt.dao.impl.facade;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cw.jeeyt.common.utils.StringUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeInfoDao;
import com.cw.jeeyt.dao.mapper.facade.FacadeInfoMapper;
import com.cw.jeeyt.dao.model.facade.FacadeInfoDo;
import com.cw.jeeyt.dao.model.facade.FacadeInfoExample;
import com.cw.jeeyt.dao.model.facade.FacadeInfoKeyDo;
import com.cw.jeeyt.dao.query.facade.FacadeInfoQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 接口信息数据库操作接口实现
 *
 * @author chenw
 * @date 2019-10-31 22:35:52
 */
@Repository
public class FacadeInfoDaoImpl extends AbstractBaseDaoImpl<FacadeInfoDo, FacadeInfoMapper> implements FacadeInfoDao {
    /**
     * 分页查询
     *
     * @param facadeInfo 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<FacadeInfoDo> selectPage(FacadeInfoQuery facadeInfo, PageParam pageParam) {
        FacadeInfoExample example = buildExample(facadeInfo);
        return PageHelper.<FacadeInfoDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询接口信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeInfoDo selectByKey(Long id) {
        FacadeInfoKeyDo facadeInfoKeyDo = new FacadeInfoKeyDo();
        facadeInfoKeyDo.setId(id);
        return getMapper().selectByPrimaryKey(facadeInfoKeyDo);
    }

    /**
     * 根据key删除接口信息
     *
     * @param id
     * @return
     */
    @Override
    public int deleteByKey(Long id) {
        FacadeInfoKeyDo facadeInfoKeyDo = new FacadeInfoKeyDo();
        facadeInfoKeyDo.setId(id);
        return getMapper().deleteByPrimaryKey(facadeInfoKeyDo);
    }

    /**
     * 查询接口信息
     *
     * @param facadeInfo 条件
     * @return List<FacadeInfoDo>
     */
    @Override
    public List<FacadeInfoDo> selectByExample(FacadeInfoQuery facadeInfo) {
        return getMapper().selectByExample(buildExample(facadeInfo));
    }

    /**
     * 新增接口信息
     *
     * @param facadeInfo 条件
     * @return int>
     */
    @Override
    public int insertBySelective(FacadeInfoDo facadeInfo) {
        if (facadeInfo == null) {
            return -1;
        }
        if(null == facadeInfo.getId()){
            Long id = SequenceTool.nextLongId();
            facadeInfo.setId(id);
        }
        return getMapper().insertSelective(facadeInfo);
    }

    /**
     * 修改接口信息
     *
     * @param facadeInfo
     * @return
     */
    @Override
    public int updateBySelective(FacadeInfoDo facadeInfo) {
        if (facadeInfo == null) {
            return -1;
        }
        return getMapper().updateByPrimaryKeySelective(facadeInfo);
    }

    /**
     * 构建接口信息Example信息
     *
     * @param facadeInfo
     * @return
     */
    public FacadeInfoExample buildExample(FacadeInfoQuery facadeInfo) {
        FacadeInfoExample example = new FacadeInfoExample();
        FacadeInfoExample.Criteria criteria = example.createCriteria();
        if (facadeInfo != null) {
            //添加查询条件
            if (null != facadeInfo.getId()) {
                criteria.andIdEqualTo(facadeInfo.getId());
            }
            if (null != facadeInfo.getDocId()) {
                criteria.andDocIdEqualTo(facadeInfo.getDocId());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getFacadeCode())) {
                criteria.andFacadeCodeEqualTo(facadeInfo.getFacadeCode());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getFacadeName())) {
                criteria.andFacadeNameEqualTo(facadeInfo.getFacadeName());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getFacadeRequestType())) {
                criteria.andFacadeRequestTypeEqualTo(facadeInfo.getFacadeRequestType());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getFacadeVersion())) {
                criteria.andFacadeVersionEqualTo(facadeInfo.getFacadeVersion());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getFacadeUrl())) {
                criteria.andFacadeUrlEqualTo(facadeInfo.getFacadeUrl());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getOperateType())) {
                criteria.andOperateTypeEqualTo(facadeInfo.getOperateType());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getRequests())) {
                criteria.andRequestsEqualTo(facadeInfo.getRequests());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getResponses())) {
                criteria.andResponsesEqualTo(facadeInfo.getResponses());
            }
            if (StringUtil.isNotEmpty(facadeInfo.getUseMethod())) {
                criteria.andUseMethodEqualTo(facadeInfo.getUseMethod());
            }
            if (null != facadeInfo.getCreateBy()) {
                criteria.andCreateByEqualTo(facadeInfo.getCreateBy());
            }
            if (null != facadeInfo.getCreateDateBegin()) {
                criteria.andCreateDateGreaterThanOrEqualTo(facadeInfo.getCreateDateBegin());
            }
            if (null != facadeInfo.getCreateDateEnd()) {
                criteria.andCreateDateLessThanOrEqualTo(facadeInfo.getCreateDateEnd());
            }
            if (null != facadeInfo.getUpdateBy()) {
                criteria.andUpdateByEqualTo(facadeInfo.getUpdateBy());
            }
            if (null != facadeInfo.getUpdateDateBegin()) {
                criteria.andUpdateDateGreaterThanOrEqualTo(facadeInfo.getUpdateDateBegin());
            }
            if (null != facadeInfo.getUpdateDateEnd()) {
                criteria.andUpdateDateLessThanOrEqualTo(facadeInfo.getUpdateDateEnd());
            }
        }
        return example;
    }
}
